#ifndef FILE_H_
#define FILE_H_

#include <stdio.h>
#include <stdlib.h>

#if 0
import std.io;
import std.lib;
#endif

char* readfile(char* filename)
{
	FILE* file;
	long fileLength;
	char* buffer;
	size_t result;

	file = fopen(filename, "rb");

	if(file == NULL)
	{
		fputs("File error",stderr);
		exit(1);
	}

	fseek(file, 0, SEEK_END);
	fileLength = ftell(file);
	rewind(file);

	buffer = (char*) malloc((fileLength + 2));
	
	if(buffer == NULL)
	{
		fputs("Memory error",stderr);
		exit(2);
	}

	result = fread(buffer, 1, fileLength, file);

	if(result != fileLength)
	{
		fputs("Reading error", stderr);
		exit(3);
	}

	fclose(file);
	return buffer;
}

void writefile(char* filename, char* string)
{
	FILE* file = fopen(filename,"w");

	if(file == NULL)
	{
		fputs("File error",stderr);
		exit(1);
	}

	fprintf(file,"%s",string);
	fclose(file);
}

#endif
